from random import randint, choice

def generate_English_problems(grade, topic, difficulty):
    """根据不同年级生成 不同难度的英语  中译英 或 英译中
        topic 决定题型"""
    filename = './词汇/英语单词/' + grade + '_' + difficulty + '.txt'
    with open(filename, 'r', encoding='utf-8') as f:
        words_str = f.read()
    words_list = eval(words_str)
    words = [pair_word[0] for pair_word in words_list]  # 单词列表
    chs = [pair_word[1] for pair_word in words_list]  # 中文列表
    # print('Grade:  ', grade, '   Difficulty: ', difficulty, '  Number: ', len(words))
    question = ""
    answer, type = "", ""
    if difficulty == 'easy':
        type = '单词'
    elif difficulty == 'midum':
        type = '词组'
    elif difficulty == 'hard':
        type = '句子'
    if topic == 0 :
        # 非困难的题目只有两种
        ops = [0, 1]  # 0 代表生成 单词翻译的题目 ; 1 代表生成根据中文写出单词的题目
        op = choice(ops)
        if op == 0:
            question, answer = word_to_ch(type, words, chs)
        else:
            question, answer = ch_to_word(type, words, chs)

    elif topic == 2:
        question, answer = word_to_ch(type, words, chs)
    elif topic == 1:
        question, answer = ch_to_word(type, words, chs)
    else:
        question, answer = "问题请求错误", "暂无答案"

    return question, answer

def word_to_ch(type, words, chs):
    """英译汉"""
    word = choice(words)
    que_index = words.index(word)
    answer = chs[que_index]
    question = "请写出{}{}的中文释义\n___".format(type, word)
    return question, answer

def ch_to_word(type, words, chs):
    """中译英"""
    ch = choice(chs)
    que_index = chs.index(ch)
    answer = words[que_index]
    question = "请写出中文释义为 {} 的英文{}\n___".format(ch, type)
    return question, answer



def get_request(grade, topic, number, difficulty):
    problems = []
    for i in range(number):
        question, answer = generate_English_problems(grade, topic, difficulty)
        if question == "问题请求错误" and answer == "暂无答案":
            problems.append((question, answer))
            break
        while (question, answer) in problems:
            # 防止出现重复的题目
            question, answer = generate_English_problems(grade, topic, difficulty)
        problems.append((question, answer))
    questions = []
    for i, (problem, answer) in enumerate(problems, 1):
        questions.append({'question_text': f"{problem}", 'answer': f"{answer}"})
    return questions

"""
topic = [混合， 中译英， 英译汉]
英语年级从 三年级开始
三年级简单  中译英数量可达137    英译汉数量可达137      混合数量可达274
三年级中等   中译英数量可达7    英译汉数量可达7      混合数量可达14
三年级困难   中译英数量可达 39   英译汉数量可达 39      混合数量可达78   英语阅读题目数量 可达100  topic=3

四年级简单  中译英数量可达 201   英译汉数量可达 201     混合数量可达402
四年级中等   中译英数量可达38    英译汉数量可达38      混合数量可达76
四年级困难   中译英数量可达46    英译汉数量可达46      混合数量可达92    英语阅读题目数量 可达100

五年级简单  中译英数量可达 196   英译汉数量可达 196     混合数量可达392
五年级中等   中译英数量可达82    英译汉数量可达82      混合数量可达164
五年级困难   中译英数量可达46    英译汉数量可达46      混合数量可达92    英语阅读题目数量 可达100

六年级简单  中译英数量可达96    英译汉数量可达 96     混合数量可达192
六年级中等   中译英数量可达41    英译汉数量可达41      混合数量可达82
六年级困难   中译英数量可达44    英译汉数量可达44      混合数量可达88     英语阅读题目数量 可达100
"""

if __name__ == '__main__':
    Grades = ['三年级', '四年级', '五年级', '六年级']

    for grade in Grades:
        questions = get_request(grade, 3, 2, 'easy')
        for q in questions:
            print(q)